//package com.qqhf.security;
//
//import cn.qiuqiu.food.bean.constant.state.ManagerStatus;
//import cn.qiuqiu.food.bean.core.ShiroUser;
//import cn.qiuqiu.food.bean.entity.front.Shop;
//import cn.qiuqiu.food.bean.entity.system.Role;
//import cn.qiuqiu.food.bean.entity.system.User;
//import cn.qiuqiu.food.bean.vo.SpringContextHolder;
//import cn.qiuqiu.food.cache.TokenCache;
//import cn.qiuqiu.food.dao.system.MenuRepository;
//import cn.qiuqiu.food.dao.system.RoleRepository;
//import cn.qiuqiu.food.dao.system.UserRepository;
//import cn.qiuqiu.food.service.system.impl.ConstantFactory;
//import cn.qiuqiu.food.utils.Constants;
//import cn.qiuqiu.food.utils.Convert;
//import cn.qiuqiu.food.utils.HttpKit;
//import org.apache.shiro.authc.CredentialsException;
//import org.apache.shiro.authc.LockedAccountException;
//import org.apache.shiro.authc.SimpleAuthenticationInfo;
//import org.apache.shiro.crypto.hash.Md5Hash;
//import org.apache.shiro.util.ByteSource;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.DependsOn;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.ArrayList;
//import java.util.HashSet;
//import java.util.List;
//import java.util.Set;
//
//@Service
//@DependsOn("springContextHolder")
//@Transactional(readOnly = true)
//public class ShiroFactroy {
//
//    @Autowired
//    private UserRepository userRepository;
//
//    @Autowired
//    private MenuRepository menuRepository;
//    @Autowired
//    private RoleRepository roleRepository;
//    @Autowired
//    private TokenCache tokenCache;
//
//    public static ShiroFactroy me() {
//        return SpringContextHolder.getBean(ShiroFactroy.class);
//    }
//
//
//    public User user(String account) {
//
//        User user = userRepository.findByAccount(account);
//
//        // 账号不存在
//        if (null == user) {
//            throw new CredentialsException();
//        }
//        // 账号被冻结
//        if (user.getStatus() != ManagerStatus.OK.getCode()) {
//            throw new LockedAccountException();
//        }
//        return user;
//    }
//
//
//    public ShiroUser shiroUser(User user) {
//        ShiroUser shiroUser = tokenCache.getUser(HttpKit.getToken());
//        if (shiroUser != null) {
//            return shiroUser;
//        }
//        shiroUser = new ShiroUser();
//        shiroUser.setId(Long.valueOf(user.getId()));            // 账号id
//        shiroUser.setAccount(user.getAccount());// 账号
//        shiroUser.setDeptId(user.getDeptid());    // 部门id
//        shiroUser.setDeptName(ConstantFactory.me().getDeptName(user.getDeptid()));// 部门名称
//        shiroUser.setName(user.getName());        // 用户名称
//        shiroUser.setPassword(user.getPassword());
//        Long[] roleArray = Convert.toLongArray(",", user.getRoleid());
//        List<Long> roleList = new ArrayList<Long>();
//        List<String> roleNameList = new ArrayList<String>();
//        List<String> roleCodeList = new ArrayList<String>();
//        Set<String> permissions = new HashSet<String>();
//        Set<String> resUrls = new HashSet<>();
//        for (Long roleId : roleArray) {
//            roleList.add(roleId);
//            Role role = roleRepository.getOne(roleId);
//            roleNameList.add(role.getName());
//            roleCodeList.add(role.getTips());
//            permissions.addAll(menuRepository.getResCodesByRoleId(roleId));
//            resUrls.addAll(menuRepository.getResUrlsByRoleId(roleId));
//
//        }
//        shiroUser.setRoleList(roleList);
//        shiroUser.setRoleNames(roleNameList);
//        shiroUser.setRoleCodes(roleCodeList);
//        shiroUser.setPermissions(permissions);
//        shiroUser.setUrls(resUrls);
//        tokenCache.setUser(HttpKit.getToken(), shiroUser);
//        return shiroUser;
//    }
//
//    public ShiroUser shiroUser(Shop shop) {
//        ShiroUser shiroUser = tokenCache.getUser(HttpKit.getToken());
//        if (shiroUser != null) {
//            return shiroUser;
//        }
//        shiroUser = new ShiroUser();
//        shiroUser.setId(Long.valueOf(shop.getId()));            // 账号id
//        shiroUser.setAccount(shop.getName());// 账号
////        shiroUser.setDeptId(user.getDeptid());    // 部门id
////        shiroUser.setDeptName(ConstantFactory.me().getDeptName(user.getDeptid()));// 部门名称
//        shiroUser.setName(shop.getName());        // 用户名称
//        shiroUser.setPassword(shop.getPassword());
//        Long[] roleArray = new Long[]{Constants.ROLE_ID_SHOP};
//        List<Long> roleList = new ArrayList<Long>();
//        List<String> roleNameList = new ArrayList<String>();
//        List<String> roleCodeList = new ArrayList<String>();
//        Set<String> permissions = new HashSet<String>();
//        Set<String> resUrls = new HashSet<>();
//        for (Long roleId : roleArray) {
//            roleList.add(roleId);
//            Role role = roleRepository.getOne(roleId);
//            roleNameList.add(role.getName());
//            roleCodeList.add(role.getTips());
//            permissions.addAll(menuRepository.getResCodesByRoleId(roleId));
//            resUrls.addAll(menuRepository.getResUrlsByRoleId(roleId));
//
//        }
//        shiroUser.setRoleList(roleList);
//        shiroUser.setRoleNames(roleNameList);
//        shiroUser.setRoleCodes(roleCodeList);
//        shiroUser.setPermissions(permissions);
//        shiroUser.setUrls(resUrls);
//        tokenCache.setUser(HttpKit.getToken(), shiroUser);
//        return shiroUser;
//    }
//
//    public SimpleAuthenticationInfo info(ShiroUser shiroUser, User user, String realmName) {
//        String credentials = user.getPassword();
//        // 密码加盐处理
//        String source = user.getSalt();
//        ByteSource credentialsSalt = new Md5Hash(source);
//        return new SimpleAuthenticationInfo(shiroUser, credentials, credentialsSalt, realmName);
//    }
//
//}
